package com.gandong8.derby.controller;

import com.gandong8.derby.core.constant.DOConstant;
import com.gandong8.derby.core.response.ResponseData;
import com.gandong8.derby.core.util.RequestUtil;
import com.gandong8.derby.pojo.vo.request.WebSiteReqVO;
import com.gandong8.derby.pojo.vo.response.WebSiteResVO;
import com.gandong8.derby.service.WebSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 网站管理控制层
 * @author xiaoyutou
 */
@Controller
public class SiteController extends BaseController{
    @Autowired
    WebSiteService webSiteService;

    @RequestMapping(value = "/site/page/list")
    @ResponseBody
    public ModelAndView listPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("site/list.html");
        return modelAndView;
    }
    @RequestMapping(value = "/site/page/add")
    @ResponseBody
    public ModelAndView addPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("site/add.html");
        return modelAndView;
    }
    @RequestMapping(value = "/site/page/edit")
    @ResponseBody
    public ModelAndView editPage(WebSiteReqVO webSiteReqVO) {
        ModelAndView modelAndView = new ModelAndView();
        WebSiteResVO resVO = webSiteService.siteInfo(webSiteReqVO);
        if(resVO == null) {
            modelAndView.setViewName("site/error.html");
            return modelAndView;
        }
        modelAndView.addObject("obj", resVO);
        modelAndView.setViewName("site/edit.html");
        return modelAndView;
    }

    @RequestMapping(value = "/pwd/index")
    @ResponseBody
    public ModelAndView pwdPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("secret.html");
        return modelAndView;
    }



    @RequestMapping(value = "/site/ajax/list")
    @ResponseBody
    public ResponseData ajaxList( WebSiteReqVO webSiteReqVO) {
        return webSiteService.siteList(webSiteReqVO);
    }


    @RequestMapping(value = "/site/ajax/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData ajaxAdd( @RequestBody WebSiteReqVO webSiteReqVO) {
        webSiteService.siteAdd(webSiteReqVO);
        return ResponseData.succ();
    }

    @RequestMapping(value = "/site/ajax/edit",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData ajaxEdit( @RequestBody WebSiteReqVO webSiteReqVO) {
        webSiteService.siteEdit(webSiteReqVO);
        return ResponseData.succ();
    }

    @RequestMapping(value = "/site/ajax/del",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData ajaxDel( WebSiteReqVO webSiteReqVO) {
        webSiteService.siteDel(webSiteReqVO);
        return ResponseData.succ();
    }

    @RequestMapping(value = "/pwd/ajax/add",method = RequestMethod.GET)
    @ResponseBody
    public ResponseData ajaxAdd( String secret) {
        if(secret==null) {
            return ResponseData.fail();
        }
        RequestUtil.setSession(DOConstant.SITE_SECRET, secret);
        return ResponseData.succ();
    }



}
